package com.briup.jd2407cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.briup.jd2407cms.domain.Slideshow;
import com.briup.jd2407cms.exception.ServiceException;
import com.briup.jd2407cms.service.SlideshowService;
import com.briup.jd2407cms.mapper.SlideshowMapper;
import com.briup.jd2407cms.util.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
* @author jay
* @description 针对表【cms_slideshow】的数据库操作Service实现
* @createDate 2024-11-18 14:40:14
*/
@Slf4j
@Service
public class SlideshowServiceImpl extends ServiceImpl<SlideshowMapper, Slideshow>
    implements SlideshowService{

    @Autowired
    private SlideshowMapper slideshowMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public List<Slideshow> queryAllEnable() {
        List<Slideshow> slideshows = slideshowMapper.selectList(
                new LambdaQueryWrapper<Slideshow>().eq(Slideshow::getStatus, "启用"));
        if (slideshows == null || slideshows.isEmpty()){
            log.error("查詢所有已啟用的輪播圖的集合為空");
            throw new ServiceException(ResultCode.SLIDESHOW_NOT_EXISTED);
        }
        return slideshows;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public IPage<Slideshow> queryByPageCondition(Integer pageNum, Integer pageSize, String status, String description) {
        //1 创建IPage分页对象,设置分页参数
        IPage<Slideshow> page = new Page<>(pageNum, pageSize);

        //2 执行分页查询
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!StrUtil.isEmpty(status), Slideshow::getStatus,status)
                .like(!StrUtil.isEmpty(description),Slideshow::getDescription,description)
                .orderByDesc(Slideshow::getUploadTime);

        //3 获取分页结果
        IPage<Slideshow> slideshowIPage = slideshowMapper.selectPage(page, lqw);

        if (slideshowIPage.getTotal() == 0)
            throw new ServiceException(ResultCode.DATA_NONE);

        return slideshowIPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(Slideshow slideshow) {
        Integer id = slideshow.getId();

        //1.判断轮播图url是否唯一
        String url = slideshow.getUrl();
        //url唯一标识
        boolean flag = false;
        if (url != null) {
            //判断是否是原来的轮播图url
            if (id != null) {
                Slideshow oldSlideshow = slideshowMapper.selectById(id);
                if (oldSlideshow != null && url.equals(oldSlideshow.getUrl())) {
                    flag = true;
                }
            }

            //判断url是否唯一
            if (!flag) {
                LambdaQueryWrapper<Slideshow> qw = new LambdaQueryWrapper<>();
                qw.eq(Slideshow::getUrl, url);
                Slideshow s = slideshowMapper.selectOne(qw);
                if (s != null) {
                    log.error("轮播图url已存在");
                    throw new ServiceException(ResultCode.SLIDESHOW_URL_EXISTED);
                }
                // 重置图片url更新时间
                slideshow.setUploadTime(LocalDateTime.now());
            }
        }

        if (id == null) {
            //2.新增操作
            if (slideshow.getStatus() == null)
                slideshow.setStatus("启用");
            slideshowMapper.insert(slideshow);
        } else {
            //3.更新操作
            //3.1 判断当前轮播图是否有效
            Slideshow s = slideshowMapper.selectById(id);
            if (s == null) {
                log.error("更新轮播图无效");
                throw new ServiceException(ResultCode.SLIDESHOW_NOT_EXISTED);
            }
            //3.2 更新操作
            slideshowMapper.updateById(slideshow);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean removeByIds(Collection<? extends Serializable> ids) {
        if (ids == null ||ids.isEmpty()){
            log.error("删除轮播图，id为空");
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        //根据ids查找轮播图
        LambdaQueryWrapper<Slideshow> qw = new
                LambdaQueryWrapper<>();
        qw.in(Slideshow::getId, ids);
        int len = slideshowMapper.selectCount(qw);
        log.info("len:{}",len);
        //如果连1个有效的id都没有，则抛出异常
        if (len <= 0) {
            log.error("要删除的轮播图不存在");
            throw new ServiceException(ResultCode.SLIDESHOW_NOT_EXISTED);
        }
        int i = slideshowMapper.deleteBatchIds(ids);
        return SqlHelper.retBool(i);
    }
}






